<template>
  <el-card class="box-card">
    <!-- 按钮 -->
    <el-button type="primary" :icon="Plus" @click="dialogVisible = true"
      >添加</el-button
    >

    <!-- 表单 -->
    <el-table
      :data="trademarkList"
      border
      style="width: 100%"
      class="el-table-margin"
    >
      <el-table-column type="index" label="序号" width="70" align="center" />
      <el-table-column prop="tmName" label="品牌名称" />
      <el-table-column label="品牌LOGO">
        <template #default="{ row }">
          <img :src="row.logoUrl" :alt="row.logoUrl" class="trademark-img" />
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column prop="address" label="操作">
        <template #default="row">
          <el-tooltip content="修改品牌" placement="top" effect="light">
            <el-button type="warning" :icon="Edit" size="small"></el-button>
          </el-tooltip>
          <el-tooltip content="修改品牌" placement="top" effect="light">
            <el-button type="danger" :icon="Delete" size="small"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

  <!-- 添加页面 -->
  <el-dialog v-model="dialogVisible" title="添加品牌">
    <el-form
      ref="trademarkFormRef"
      :model="trademarkFormData"
      :rules="trademarkFormRules"
      label-width="100px"
    >
      <el-form-item label="品牌名称" prop="tmName">
        <el-input class="trademark-input" v-model="trademarkFormData.tmName" />
      </el-form-item>

      <el-form-item label="品牌LOGO" prop="logoUrl">
        <el-upload
          class="avatar-uploader"
          action="/app-dev/admin/product/fileUpload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="trademarkFormData.logoUrl"
            :src="trademarkFormData.logoUrl"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">确定</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >取消</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
export default {
  name: "Trademark",
};
</script>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import type { UploadProps, FormRules, FormInstance } from "element-plus";
import type { TrademarkList } from "@/api/product/model/trademarkModel";
import { getTrademarkListApi } from "@/api/product/trademark";

const tableData = ref<TrademarkList>([]);

const trademarkFormRef = ref<FormInstance>();
// 当前页码
const currentPage = ref(1);
// 每页条数
const pageSize = ref(3);
// 总数
const total = ref(0);

const imageUrl = ref("");
// 得到表单数据
const trademarkFormData = reactive({
  tmName: "",
  logoUrl: "",
});

// 表单效验规则
const trademarkFormRules = reactive<FormRules>({
  tmName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  logoUrl: [{ required: true, message: "请上传品牌logo", trigger: "blur" }],
});

// 当前页码发生变化时触发
const handleCurrentChange = (newCurrentPage: number) => {
  currentPage.value = newCurrentPage;
  // 更新数据
  getTrademarkList();
};
// 当每页条数发生变化时触发
const handleSizeChange = (newPageSize: number) => {
  pageSize.value = newPageSize;
  // 更新数据
  getTrademarkList();
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  trademarkFormData.logoUrl = response.data;
};
const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be JPG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
// 添加品牌(控制显示和隐藏)
const dialogVisible = ref(false);

// const handleClose = (done: () => void) => {
//   ElMessageBox.confirm("Are you sure to close this dialog?")
//     .then(() => {
//       done();
//     })
//     .catch(() => {
//       // catch error
//     });
// };

// 品牌数据
const trademarkList = ref<TrademarkList>([]);
// 封装一个用来发送请求，获取品牌列表的函数
const getTrademarkList = async () => {
  const result = await getTrademarkListApi(currentPage.value, pageSize.value);
  // 将数据更新
  trademarkList.value = result.records;
  total.value = result.total;
  console.log(trademarkList.value);
}; //刚才这两个地方写的是个 逗号
onMounted(() => {
  getTrademarkList();
});
</script>

<style scoped>
.el-table-margin {
  margin: 20px 0;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.trademark-img {
  width: 150px;
  height: 100px;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
</style>
